package utils

import "github.com/hashicorp/consul/api"

type ConsulClient struct {
	client *api.Client
}
type Consul struct {
	ID      string
	Name    string
	Address string
	Port    int
	Tags    []string
}

func (c *ConsulClient) Register(consul *Consul) error {
	registration := &api.AgentServiceRegistration{
		ID:      consul.ID,
		Name:    consul.Name,
		Address: consul.Address,
		Port:    consul.Port,
		Tags:    consul.Tags,
	}
	return c.client.Agent().ServiceRegister(registration)
}

func NewConsulClient(addr string) (*ConsulClient, error) {
	config := api.DefaultConfig()
	config.Address = addr
	client, err := api.NewClient(config)
	if err != nil {
		return nil, err
	}
	return &ConsulClient{client: client}, nil
}
